package tech.dongliang.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import tech.dongliang.common.controller.CommonController;
import tech.dongliang.common.enums.EBizError;
import tech.dongliang.common.response.CommonResponse;
import tech.dongliang.common.response.ErrorData;
import tech.dongliang.system.model.SysRole;
import tech.dongliang.system.service.ISysRoleService;

import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xdzhang
 * @since 2019-01-18
 */
@Controller
@RequestMapping("/system/role")
public class SysRoleController extends CommonController {
    private String prefix = "system/role";
    @Autowired
    private ISysRoleService sysRoleService;

    @GetMapping()
    String menu() throws Exception {
        return prefix + "/list";
    }

    @ResponseBody
    @GetMapping("/list")
    public CommonResponse<IPage<SysRole>> list(@RequestParam(value = "page", required = false, defaultValue = "1") Long page,
                                               @RequestParam(value = "roleName", required = false) String roleNanme) throws Exception {
        IPage<SysRole> pager = new Page<>();
        pager.setPages(page);
        QueryWrapper<SysRole> query = new QueryWrapper();
        if (StringUtils.isNotBlank(roleNanme)) {
            query.eq("name", roleNanme);
        }
        pager = sysRoleService.page(pager, query);
        return CommonResponse.success(pager);
    }

    @GetMapping("/add")
    public String add(Model model) throws Exception {
        return prefix + "/add";
    }

    @GetMapping("/edit/{roleId}")
    public String edit(@PathVariable("roleId") Integer roleId, Model model) throws Exception {
        SysRole sysRole = sysRoleService.getById(roleId);
        model.addAttribute("sysRole", sysRole);
        return prefix + "/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    public CommonResponse<Boolean> save(@Valid SysRole sysRole, BindingResult result) throws Exception {
        sysRole.setCreateTime(System.currentTimeMillis());
        if (sysRoleService.save(sysRole)) {
            return CommonResponse.success(true);
        }
        return CommonResponse.error(new ErrorData(EBizError.UNKNOWE_ERROR));
    }

    /**
     * 修改
     */
    @ResponseBody
    @PostMapping("/update")
    public CommonResponse<Boolean> update(@Valid SysRole sysRole, BindingResult result) throws Exception {
        sysRole.setUpdateTime(System.currentTimeMillis());
        if (sysRoleService.updateById(sysRole)) {
            return CommonResponse.success(true);
        }
        return CommonResponse.error(new ErrorData(EBizError.UNKNOWE_ERROR));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    public CommonResponse<Boolean> remove(Integer deptId) throws Exception {
        if (sysRoleService.removeById(deptId)) {
            return CommonResponse.success(true);
        }
        return CommonResponse.error(new ErrorData(EBizError.UNKNOWE_ERROR));
    }

    @GetMapping("/menu/{roleId}")
    public String add(Model model, @PathVariable("roleId") Integer roleId) throws Exception {
        model.addAttribute("roleId", roleId);
        return prefix + "/menuTree";
    }
}
